<template>
  <div id="indexPage" class="flex indexPage">
    <div class="box-placeholder">
      <HeaderPage ref="HeaderPageRef" :type="data.gameType" @query="queryItem" />
    </div>
    <div class="home">
      <!-- 顶部Icon -->
      <div class="box-icon flexRightDetween paddingB10">
        <div class="flex">
          <div class="flex marginR5">
            <img :src="selectToLabel(GameLang, data.gameLang, 'icon', 'value')" alt=""
              style="width: 30px;height: 20px;">
            <p class="game-title marginL5">{{ selectToLabel(GameLang, data.gameLang, 'label', 'value') }}</p>
          </div>
          <div class="icon_item textCenter margin5" v-for="(item, index) in data.iconList"
            @click="data.isShowLang = !data.isShowLang">
            <img :src="item" alt="">
          </div>
        </div>
        <div class="box_lang" v-show="data.isShowLang">
          <div class="flex lang_item" v-for="(item, index) in GameLang" :key="item.value" @click="chooseLang(item)">
            <img :src="item.icon" alt="">
            <p class="width80">{{ item.label }}</p>
          </div>
        </div>
      </div>
      <!-- 走马灯 -->
      <div class="flexBetween">
        <el-carousel indicator-position="outside" height="140px" class="width100">
          <el-carousel-item class="flexAround" v-for="(item, index) in RoundPCList" :key="index">
            <img class="round_item" :src="second" alt="" v-for="(second,two) in item.label" :key="two">
          </el-carousel-item>
        </el-carousel>
      </div>
      <!-- 游戏类型 -->
      <div class="flexBetween marginT20 marginB10 box_type scrollStyle" ref="scrollContainer" @wheel="handleWheel">
        <div class="textCenter game_type" :class="data.gameType == item.value ? 'selectColorType' : ''"
          v-for="(item, index) in GameType" @click="chooseType(item)">
          <div class="textCenter"><img :src="item.icon" alt=""></div>
          <p>{{ item.label }}</p>
        </div>
      </div>
      <!-- 搜索 -->
      <div class="flexBetween box_brand marginB20">
        <div class="flex">
          <div style="position: relative;">
            <input class="input" v-model="data.gameName" icon="Search" placeholder="Search game" />
            <img class="magnifying" :src="Magnifying" alt="">
          </div>
          <img class="search marginL10" :src="Searchs" alt="" @click="searchGame">
        </div>
        <div class="flex">
          <div class="textCenter game_brand marginR10"
            :class="data.indexBrand == index ? 'selectColorGame animate__bounceIn' : ''"
            v-for="(item, index) in GameBrand" @click="chooseBrand(item, index)" v-show="item.show">
            <img :src="item.icon" alt="" :class="{ 'marginT2': index != 5 }">
          </div>
        </div>
      </div>
      <!-- 热门游戏展示 -->
      <p class="box-title marginB5">JACKPOT<span class="box-text">{{ HotGame.length }} Games</span></p>
      <div class="width100 hot_game flex scrollStyle" ref="scrollGame" @wheel="handleWheelGame">
        <div class="choose_game" v-for="(item, index) in HotGame" :key="index" @click="chooseGame(item)" style="height: 210px;">
          <div class="textCenter marginR10 game_item">
            <img class="game-img" :src="item.label" alt="">
            <p class="game-title">{{ item.remark }}</p>
            <p class="remark">{{ item.brand }} Game</p>
            <!-- 游戏蒙层 -->
            <div class="selectedEffect textCenter">
              <img v-if="checkGame(item.value)" class="player an_breathe" :src="Player" alt="">
              <div v-else class="player_no">Stay tuned</div>
            </div>
          </div>
        </div>
      </div>
      <!-- 品牌游戏 -->
      <div class="width100 box_game marginB20" v-for="(item, index) in data.list" :key="index"
        :id="selectToLabel(GameBrand, item.brand)">
        <p class="box-title marginB15" v-show="data.gameType == 1 || item.value == data.gameType ? true : false">{{
          item.remark }} <span class="box-text">{{ item.label.length }} Games</span></p>
        <div class="flexWrap width100" v-show="data.gameType == 1 || item.value == data.gameType ? true : false">
          <div class="choose_game" v-for="(second, two) in item.label" :key="two" @click="chooseGame(second)">
            <div class="textCenter marginR10 game_item" :class="{'animate__bounceIn':data.searchGame==second.value?true:false}">
              <img class="game-img" :src="second.label" alt="">
              <div class="game-title text-cut">{{ second.remark }}</div>
              <p class="remark">{{ item.remark }}</p>
              <!-- 游戏蒙层 -->
              <div class="selectedEffect textCenter">
                <img v-if="checkGame(second.value)" class="player an_breathe" :src="Player" alt="">
                <div v-else class="player_no">Stay tuned</div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <!-- 加载中 -->
      <div id="layer-loading" class="layui-m-layer layui-m-layer2" index="0" v-if="data.loading">
        <div class="layui-m-layershade"></div>
        <div class="layui-m-layermain">
          <div class="layui-m-layersection">
            <div class="layui-m-layerchild layui-m-anim-scale">
              <div class="layui-m-layercont">
                <i></i>
                <i class="layui-m-layerload"></i>
                <i></i>
              </div>
            </div>
          </div>
        </div>
      </div>
      <!-- 回到顶部 -->
      <img class="back_top an_breathe" :src="Back" alt="back top" v-show="data.scrollTopValue > 600 ? true : false"
        @click="backTop">
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, nextTick, onUnmounted, computed } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import HeaderPage from '@/components/HeaderPage.vue'
import {
  GameType, GameBrand, GamePP, GamePG, GameJILI, LogoUrl, GameMine,
  GameHacksaw , GameOMG, GameSPRIBE, HotGame, GameLang, RoundPCList
} from '@/config/eunm'
import { throttle, selectToLabel, getOperatorInfo } from '@/utils/index'
import { getGame, playGame } from '@/api/game'
import { userLogin } from '@/api/user'

// Icon
import Me from "@/assets/icon/yanshi_com_icon11.png" //我的
import News from "@/assets/icon/yanshi_com_icon13.png" //消息
import Lang from "@/assets/icon/yanshi_com_icon12.png" //语言
import Searchs from "@/assets/icon/yanshi_com_icon15.png" //搜索
import Player from "@/assets/icon/yanshi_bofang.png" //点击游戏
import Back from "@/assets/icon/yanshi_zhiding.png" //回到顶部
import Magnifying from "@/assets/icon/yanshi_com_icon16.png" //放大镜

const data = reactive({
  iconList: [Lang],
  gameType: 1,
  indexBrand: -1,
  gameName: '',
  gameLang: 'en',
  isShowLang: false,
  list: [
    { label: GameOMG, value: 1, remark: selectToLabel(LogoUrl,getOperatorInfo(location.hostname),'remark','value')+' Games', brand: selectToLabel(LogoUrl,getOperatorInfo(location.hostname),'remark','value')},
    { label: GamePG, value: 2, remark: 'PG Games', brand: 'PG' },
    { label: GameJILI, value: 2, remark: 'JILI Games', brand: 'JILI' },
    { label: GameMine, value: 3, remark: 'Mine Games', brand: 'MineGame' },
    { label: GamePP, value: 2, remark: 'PP Games', brand: 'PRAGMATIC' },
    { label: GameSPRIBE, value: 3, remark: 'SPRIBE Games', brand: 'SPRIBE' },
    // { label: GameHacksaw, value: 2, remark: 'Hacksaw Games', brand: 'HACKSAW' },
  ],
  addId: 0,
  liveGameData: [] as any,
  androidShow: false,
  windowShow: false,
  iphoneShow: false,
  loading: false,
  scrollTopValue: 0,
  searchGame: -1,
})

// 商户类别 + 商户
const queryItem = (value: any) => {
  GameType.forEach((item: any) => {
    if (item.value == value) {
      chooseType(item)
    }
  })
}

const FirstMeth = async() =>{
// 动态监听滚动条高度
  const scrollContainer = document.querySelector('#app') as any;
  if (scrollContainer) {
    scrollContainer.addEventListener('scroll', getScroll)
  }
  getPhoneInfo()
  getGameList()
}

// 获取可玩游戏
const getGameList = async () => {
  let res = await getGame({})
  data.addId = res.data.app_id
  data.liveGameData = res.data.games
  sessionStorage.setItem('OMGtoken', res.data.token)
}
// 选择游戏
const chooseGame = throttle(async (item: any) => {
  if (data.loading) return
  data.loading = true
  // 第三方游戏
  let token = '';
  if(item.value > 10000000){
    let resToken = await userLogin({
      gameid: JSON.stringify(item.value),
      lang: data.gameLang,
      nick: JSON.stringify(item.value),
      appid: JSON.stringify(data.addId),
      token: sessionStorage.getItem('OMGtoken'),
    });
    token = resToken.data.token
  }
  try {
    let res = await playGame({
      gameid: JSON.stringify(item.value),
      lang: data.gameLang,
      nick: JSON.stringify(item.value),
      appid: JSON.stringify(data.addId),
      token: token?token:sessionStorage.getItem('OMGtoken'),
    })
    data.loading = false
    if (res.data.gameurl) {
      if (data.iphoneShow) {
        window.location.href = res.data.gameurl;
      } else {
        window.open(res.data.gameurl);
      }
    } else {
      ElMessage.error('Stay tuned')
    }
  } catch (error) {
    data.loading = false
  }
}, 2000)

// 查询当前游戏是否开放
const checkGame = (id: any) => {
  return data.liveGameData.find((item: any) => item.id == id)
}

// 获取当前设备信息
const getPhoneInfo = () => {
  var userAgent = navigator.userAgent;
  if (/Android/.test(userAgent)) {
    data.androidShow = true;
  } else {
    data.androidShow = false;
  }
  if (/Windows/.test(userAgent)) {
    data.windowShow = true;
  } else {
    data.windowShow = false;
  }
  if (/iPhone/.test(userAgent)) {
    data.iphoneShow = true;
  } else {
    data.iphoneShow = false;
  }
}

// 搜索游戏
const searchGame = () => {
  let gameArrList = [] as any;
  data.searchGame = -1
  if(!data.gameName){
    return ElMessage.error('Please enter your search')
  }
  data.list.forEach((item:any)=>{
    item.label.forEach((second:any) => {
      gameArrList.push(second)
    })
  })
  let game = gameArrList.filter((item:any) =>
    item.remark.toLowerCase().includes(data.gameName.toLowerCase())
  )
  if(game[0]){
    GameBrand.forEach((item:any,index:number)=>{
      if(game[0].type==item.value){
        chooseBrand(item,index,game[0].value)
      }
    })
  }else{
    ElMessage.error('No corresponding information found')
  }
}

// 选择游戏类型
const chooseType = (item: any) => {
  data.gameType = item.value
  data.indexBrand = -1
  var element = document.getElementById('app') as any;
  if (element) {
    element.scrollTop = 0
  }
  // Originals【全部】 Slots【PG,JILI,PP】 MiniGame【OMG,SPRIBE】
  GameBrand.forEach((second: any) => {
    second.show = false
    if (item.label == 'Originals') {
      second.show = true
    }
    if (item.label == 'Slots') {
      if (second.label == 'PG' || second.label == 'JILI' || second.label == 'PRAGMATIC' || second.label == 'HACKSAW') {
        second.show = true
      }
    }
    if (item.label == 'Mini Game') {
      if (second.label == 'GM' || second.label == 'OMG' || second.label == 'SPRIBE') {
        second.show = true
      }
    }
  })
}

// 选择游戏品牌
const chooseBrand = (item: any, index: number, game?:number) => {
  data.indexBrand = index
  // 设置滚动条高度
  var home = document.getElementById('app') as any;
  var element = document.getElementById(item.label) as any;
  console.log(element);
  const rect = element.getBoundingClientRect();
  const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
  const offsetTop = rect.top + scrollTop;
  home.scrollTop = offsetTop - 60
  if(game){
    data.searchGame = game
  }
  data.searchGame
  setTimeout(function() {
    data.searchGame = -1
  },1000);
}

// 获取滚动条高度
const getScroll = (event: any) => {
  data.scrollTopValue = event.target.scrollTop
}

// 返回顶部
const backTop = () => {
  var element = document.getElementById('app') as any;
  element.scrollTo({
    top: 0,
    left: 0,
    behavior: 'smooth' // 使滚动平滑
  })
}

// 选择多语言
const chooseLang = (item: any) => {
  data.isShowLang = false
  data.gameLang = item.value
}

// 横向滚动
const scrollGame = ref();
const handleWheelGame = (event: any) => {
  // 阻止默认的滚动行为
  event.preventDefault();
  // 获取滚动的方向
  const delta = event.deltaY > 0 ? -350 : 350; // 根据需要可以调整滚动的速度
  // 滚动容器
  scrollGame.value.scrollBy({
    left: delta,
    behavior: 'smooth'
  })
}
// 横向滚动
const scrollContainer = ref();
const handleWheel = (event: any) => {
  // 阻止默认的滚动行为
  event.preventDefault();
  // 获取滚动的方向
  const delta = event.deltaY > 0 ? -250 : 250; // 根据需要可以调整滚动的速度
  // 滚动容器
  scrollContainer.value.scrollBy({
    left: delta,
    behavior: 'smooth'
  })
}

defineExpose({
  FirstMeth,
})
</script>

<style scoped lang="scss">
.indexPage {
  width: 99%;
  height: 100%;
  font-family: 'Geogtq', sans-serif;
  font-weight: bold;
  user-select: none; //禁止被用户选中
  overflow: auto;
}

.box-placeholder {
  width: 7%;
  height: 100%;
}
.scrollStyle{
  overflow-x: scroll;
  overflow-y: hidden;
  white-space: nowrap;
  padding-bottom: -10px;
  font-family: 'Graphik', sans-serif;
  &::-webkit-scrollbar {
    height: 3px;
  }
  &::-webkit-scrollbar-thumb {
    background: var(--color-select);
    border-radius: 20px;
  }
}
.home {
  width: 80%;
  height: 100%;
  margin: auto;
  padding-top: 70px;
  position: relative;
  .round_item {
    width: 31%;
    height: 140px;
    border-radius: 15px;
    img {
      width: 100%;
      height: 140px;
    }
  }

  .search {
    width: 40px;
    height: 40px;
    cursor: pointer;
  }

  .search:hover {
    transform: scale(0.9);
  }
}

.box-icon {
  width: 100%;
  height: 60px;
  background-color: var(--color-background);
  padding-right: 125px;
  position: fixed;
  top: 0;
  right: 0;
  z-index: 100;
  border-bottom: 2px solid var(--color-index);
  .icon_item {
    padding: 8px 12px 3px;
    background-color: var(--color-index);
    border-radius: 5px;
    cursor: pointer;

    img {
      width: 22px;
      height: 22px;
    }
  }

  .icon_item:hover {
    transform: scale(1.2);
  }

  .box_lang {
    width: 200px;
    height: 280px;
    overflow: auto;
    color: #f6f6f6;
    line-height: 30px;
    background-color: var(--color-index);
    border-radius: 5px;
    position: absolute;
    top: 50px;
    right: 0;
    z-index: 100;

    .lang_item {
      cursor: pointer;
      padding: 5px 10px;

      img {
        width: 30px;
        margin-right: 10px;
      }
    }

    .lang_item:hover {
      background-color: rgba(144, 244, 98, 0.2);
    }
  }
}
// 游戏类型
.box_type {
  .game_type {
    width: 10%;
    height: 80px;
    padding-top: 8px;
    margin-right: 5px;
    cursor: pointer;
    border-radius: 15px;
    border: 2px solid var(--color-index);
    background-color: rgba(33, 37, 41, 1);
    box-sizing: border-box;

    /* 防止边框影响尺寸 */
    img {
      width: 30px;
      height: 30px;
    }
  }

  .game_type:hover {
    color: var(--color-select);
    border: 2px solid var(--color-select);
    background-color: rgba(144, 244, 98, 0.2);

    img {
      transform: translateY(-50px);
      filter: drop-shadow(var(--color-select) 0 50px);
    }
  }
}

// 游戏品牌
.box_brand {
  .magnifying{
    position: absolute;
    top: 12px;
    left: 10px;
  }
  .game_brand {
    width: 110px;
    height: 40px;
    line-height: 40px;
    border-radius: 30px;
    border: 2px solid var(--color-index);
    cursor: pointer;
    overflow: hidden;
    box-sizing: border-box;

    /* 防止边框影响尺寸 */
    img {
      width: 60px;
      height: 30px;
    }
  }

  .game_brand:hover {
    color: var(--color-select);
    border: 2px solid var(--color-select);
    background-color: rgba(144, 244, 98, 0.2);
    box-shadow: 0.5px 0.5px 10px var(--color-select);

    img {
      transform: translateY(-50px);
      filter: drop-shadow(var(--color-select) 0 50px);
    }
  }
}

// 游戏
.box-title {
  font-family: 'Graphik', sans-serif;
  font-size: 20px;
}

.game-title {
  width: 200px;
  font-size: 16px;
  font-family: 'Geogtq', sans-serif;
  padding-left: 5px;
}

.game-img {
  width: 200px;
  height: 156px;
  border-radius: 15px;
}

.box-text {
  font-family: 'Geogtq', sans-serif;
  margin-left: 5px;
}

.player_title {
  font-size: 18px;
  font-family: 'Graphik', sans-serif;
  padding-left: 5px;
}

.remark {
  font-size: 14px;
  color: #7a7b7f;
  padding-left: 4px;
}

.player_no {
  width: 120px;
  height: 30px;
  margin: 60px auto;
  border-radius: 15px;
  background-color: rgba(237, 237, 237, 0.2);
  border: 2px solid #ededed;
}

.player {
  width: 55px !important;
  height: 55px !important;
  margin-top: 55px;
}

.player:hover {
  transform: scale(0.8);
}

.hot_game {
  width: 100%;
  height: 270px;
  overflow: auto;
  margin-bottom: 25px;
}

.back_top {
  width: 50px;
  height: 50px;
  cursor: pointer;
  position: fixed;
  bottom: 150px;
  right: 50px;
}

.back_top:hover {
  transform: scale(1.1);
}

// 蒙层效果
.selectedEffect {
  width: 100%;
  height: 156px;
  background-color: rgba(33, 37, 41, 0.8);
  overflow: hidden;
  position: absolute;
  top: 0;
  left: 0;
  opacity: 0;
  border-radius: 15px;
}

.choose_game {
  cursor: pointer;
  margin-bottom: 5px;
  font-weight: bold;
  height: 220px;
  .game_item {
    color: #FFFFFF;
    text-align: left;
    font-size: 16px;
    margin: 0 5px;
    position: relative;
    transition: .3s;
    /* 设置一个淡入淡出的效果 */
    pointer-events: none;
  }
}

.choose_game:hover .selectedEffect {
  width: 100% !important;
  height: 156px;
  opacity: 1;
}

.choose_game:hover .game_item {
  margin-top: -20px;
}
</style>